<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
//for stock

define('COLOR_NAME', 'Farve');
define('SIZE_NAME', 'Størrelse');


ini_set('max_execution_time', 900);
set_time_limit(900);
echo '<pre>';

function group_assoc($array, $key) {
    $return = array();
    foreach($array as $v) {
        $return[$v[$key]][] = $v;
    }
    return $return;
}

function toArray($obj)
{
    if (is_object($obj)) $obj = (array)$obj;
    if (is_array($obj)) {
        $new = array();
        foreach ($obj as $key => $val) {
            $new[$key] = toArray($val);
        }
    } else {
        $new = $obj;
    }
    return $new;
}

function insertProduct($db,  $myproduct){
	$supplier = $myproduct['Supplier']; 
	$itemgroup = $myproduct['ItemGroup'];
	$KatalogNumber = $myproduct['KatalogNumber'];
	$manufacturer = $db->fetch_row('supplier = ' . $supplier, 'manufacturer', FETCH_ASSOC);
	$exists = $db->get_rows();
	$category = $db->fetch_row('itemgroup = ' . $itemgroup, 'category', FETCH_ASSOC);
	$exists1 = $db->get_rows();
	
	if(!empty($exists) && !empty($exists1)){
		$manufacturer_id = $manufacturer[0]['manufacturer_id'];
		//product
		$fields = array('sku','stock_status_id','manufacturer_id','date_available','date_added', 'katalognumber' );
		$data = array( $myproduct['Kparam1'], '5', $manufacturer_id, date('Y-m-d'), date('Y-m-d'), $KatalogNumber);
		$product_id = $db->insert($fields, $data, 'product');
		
		//product_description
		$fields = array('product_id','language_id','name');
		$data = array($product_id, '1', $myproduct['Kparam2']);
		$db->insert($fields, $data, 'product_description');
		
		//product_to_category
		$category_id = $category[0]['category_id'];
		$fields = array('product_id','category_id');
		$data = array($product_id, $category_id);
		$db->insert($fields, $data, 'product_to_category');
		
		//product_to_store
		$fields = array('product_id','store_id');
		$data = array($product_id, '0');
		$db->insert($fields, $data, 'product_to_store');
	}
	return $product_id;
}

function updateProduct($db, $product_id, $myproduct){

	$supplier = $myproduct['Supplier']; 
	$itemgroup = $myproduct['ItemGroup'];
	
	$manufacturer = $db->fetch_row('supplier = ' . $supplier, 'manufacturer', FETCH_ASSOC);
	$exists = $db->get_rows();
	$category = $db->fetch_row('itemgroup = ' . $itemgroup, 'category', FETCH_ASSOC);
	$exists1 = $db->get_rows();
	
	if(!empty($exists) && !empty($exists1)){
		//product
		$manufacturer_id = $manufacturer[0]['manufacturer_id'];
		
		$fields = array('sku','stock_status_id','manufacturer_id', 'date_modified');
		$data = array( $myproduct['Kparam1'], '5', $manufacturer_id, date('Y-m-d'));
		$db->update($fields, $data, 'product_id = ' . $product_id, 'product');

		//product_description
		$fields = array('language_id','name');
		$data = array('1', $myproduct['Kparam2']);
		$db->update($fields, $data, 'product_id = ' . $product_id, 'product_description');
		
		//product_to_category
		$category_id = $category[0]['category_id'];
		$fields = array('category_id');
		$data = array($category_id);
		$db->update($fields, $data, 'product_id = ' . $product_id, 'product_to_category');
	}
	
	return $product_id;
	
}

function addColortoproduct($db, $product_id, $quantity, $attribute_id, $arr_colors){

	$fields = array('product_id','attribute_id');
	$data = array($product_id, $attribute_id);
	$product_option_id = $db->insert($fields, $data, 'product_option');
	
	$fields = array('product_option_id','language_id','product_id','name');
	$data = array($product_option_id, '1', $product_id, COLOR_NAME );
	$db->insert($fields, $data, 'product_option_description');
	
	foreach($arr_colors as $color){
		$attribute_value_id = getAttributeValueId($db, $attribute_id, $color );
		$fields = array('product_option_id','product_id','quantity','attribute_id','attribute_value_id');
		$data = array($product_option_id, $product_id, $quantity , $attribute_id, $attribute_value_id);
		$product_option_value_id = $db->insert($fields, $data, 'product_option_value');
		
		$fields = array('product_option_value_id','language_id','product_id','name');
		$data = array($product_option_value_id, '1', $product_id, $color);
		$db->insert($fields, $data, 'product_option_value_description');
	}
	
}

function addSizetoproduct($db, $product_id, $quantity, $attribute_id, $arr_size){

	$fields = array('product_id','attribute_id');
	$data = array($product_id, $attribute_id);
	$product_option_id = $db->insert($fields, $data, 'product_option');
	
	$fields = array('product_option_id','language_id','product_id','name');
	$data = array($product_option_id, '1', $product_id, SIZE_NAME );
	$db->insert($fields, $data, 'product_option_description');
	
	foreach($arr_size as $size){
		$attribute_value_id = getAttributeValueId($db, $attribute_id, $size );
		$fields = array('product_option_id','product_id','quantity','attribute_id','attribute_value_id');
		$data = array($product_option_id, $product_id, $quantity , $attribute_id, $attribute_value_id);
		$product_option_value_id = $db->insert($fields, $data, 'product_option_value');
		
		$fields = array('product_option_value_id','language_id','product_id','name');
		$data = array($product_option_value_id, '1', $product_id, $size);
		$db->insert($fields, $data, 'product_option_value_description');
	}
	
}

function getStockcount($ean, $arrStock){
	$count = '0';
	foreach($arrStock as $key=>$value){
		if($ean == $value['Key']){
			$count = $value['Value'];
			break;
		}
	}
	return $count;
}

function getAttributeId($db, $KatalogNumber, $name){
	$attribute = $db->fetch_row('katalognumber = ' . $KatalogNumber .", name = '" . $name ."'", 'attribute', FETCH_ASSOC);
	$exists = $db->get_rows();
	if(!empty($exists)){
		return $attribute[0]['attribute_id'];
	}
	return '';
}

function getAttributeValueId($db, $attribute_id, $name){
	$attribute_value = $db->fetch_row('attribute_id = ' . $attribute_id .", name = '" . $name ."'", 'attribute_value', FETCH_ASSOC);
	$exists = $db->get_rows();
	if(!empty($exists)){
		return $attribute_value[0]['value_id'];
	}
	return '';

}

require_once('simplesql.class.php5');

$ini = parse_ini_file("web.ini");
while(list($key,$value) = each($ini)){
	if($key == 'hostName'){
		$hostName = $value;
	}
	else if($key == 'userName'){
		$userName = $value;
	}
	else if($key == 'password'){
		$password = $value;
	}
	else if($key == 'dbName'){
		$dbName = $value;
	}
}

$db = new simplesql($hostName,$userName,$password,$dbName);
$db->query("SET NAMES utf8");

	$myarticles = file_get_contents('cache.txt');
	$objInfo = unserialize($myarticles);
	$arrInfo = toArray($objInfo);
	
	$mystock = file_get_contents('stock.txt');
	$objStock = unserialize($mystock);
	$arrStock = toArray($objStock);
	
	
	
	$action = isset($_GET['action'])?$_GET['action']:"insert";
	
	$group_products = group_assoc($arrInfo, 'KatalogNumber');

	$error = false;
	$db->query("BEGIN");
	foreach($group_products as $KatalogNumber=>$products){
		$product = $products[0];
		//$ean = $product['Ean'];
		$product_row = $db->fetch_row('katalognumber = ' . $KatalogNumber, 'product', FETCH_ASSOC);
		$exists = $db->get_rows();
		
		//echo $db->get_lastquery() . "<br />";
		//echo $ean . "---" . $exists . "---" . $product['ItemGroup'] . "---" . $product['Kparam2'] . "<br />";
		
		//var_dump($product);
		//$quantity = getStockcount($ean , $arrStock);
		
		//echo $ean ."---";
		//echo $quantity . "<br />";
		
		
		//$group_product_color = group_assoc($products, 'Vparam1');
		//$arr_colors = array_keys($group_product_color);
		
		//$group_product_size = group_assoc($products, 'Vparam4');
		//$arr_sizes = array_keys($group_product_size);
		
		//$color_attribute_id = getAttributeId($db, $KatalogNumber, COLOR_NAME);
		//$size_attribute_id = getAttributeId($db, $KatalogNumber, SIZE_NAME);
		
		if(empty($exists)){
			$product_id = insertProduct($db, $product);
		}else{
			$product_id = $product_row[0]['product_id'];
			updateProduct($db, $product_id, $product);
		}
		
		//$db->delete('product_id = ' . $product_id, 'product_option');
		//$db->delete('product_id = ' . $product_id, 'product_option_description');
		//$db->delete('product_id = ' . $product_id, 'product_option_value');
		//$db->delete('product_id = ' . $product_id, 'product_option_value_description');
		//addColortoproduct($db, $product_id, $quantity, $color_attribute_id, $arr_colors);
		//addSizetoproduct($db, $product_id, $quantity, $size_attribute_id, $arr_sizes);
		
		
	}
	
	if(!$error)  
	{  
	//*** Commit Transaction ***//  
		$db->query("COMMIT");  
		echo "OK <br />";  
		
	}  
	else  
	{  
		//*** RollBack Transaction ***//  
		echo "Error:  [".$db->get_lastquery()."]" ."-" . mysql_error();  
		$db->query("ROLLBACK");  
	}
	
	echo '</pre>';


?>
</body>
</html>